﻿using stdCall.File;
using stdCall.MyExcel;
using stdCall.Resource;
using stdCall;
using System;
using System.Windows.Forms;

namespace stdCall.MyExcel
{
    /// <summary>
    /// 提供导入excel模块的服务
    /// </summary>
    class ExcelService
    {
        ExcelHelper excelHelper ;
        /// <summary>
        /// 倒入excel主函数
        /// </summary>
        /// <returns></returns>
        public bool loadExcel(){
            bool flag = false;
            try
            {
                OpenFileDialog ofd = new OpenFileDialog();                //new一个方法
                ofd.Filter = "excel文件(*.xls)|*.xls|所有文件(*.*)|*.*";
                //  ofd.InitialDirectory = Environment.GetFolderPath_r(Environment.SpecialFolder.MyDocuments); //定义打开的默认文件夹位置
                ofd.ShowDialog();
                if (ofd.FileName == null || ofd.FileName == "") return false;
                excelHelper = new ExcelHelper(ofd.FileName);
                FileHelper fileHelper = new FileHelper();
                fileHelper.openWriteFile(ResourseLoader.FileSTDSPath, false);
                Array stds = excelHelper.GetContent("Sheet1");
                excelHelper.Close();
                int y = 1;
                do
                {
                    String stdnum = (stds.GetValue(y, 1)).ToString();
                    fileHelper.writeSingleData(stdnum);
                    String stdname = stds.GetValue(y, 2).ToString().Trim();
                    fileHelper.writeSingleData(stdname);
                    y++;
                } while (stds.GetValue(y, 1) != null);
                fileHelper.closeFile();
                flag = true;
            }
            catch (System.Exception ex)
            {
                flag = false;
            }
            return flag;
        }
    }
}
